package  
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.system.Capabilities;
	
	
	import starling.display.Image;
	import starling.display.Sprite;	
	import starling.textures.Texture;
	
	public class StarlingSprite extends Sprite
	{
		private var bg_gradient:Image;
		private var parallax_layers:Vector.<Image>;
		private var PARALLAX_LAYER_NUM:uint = 2;
		private var STAR_NUMBER:uint = 400;		
		private static var instance:StarlingSprite;
		
		public function StarlingSprite() 
		{
			instance = this;
			this.parallax_layers = new Vector.<Image>(PARALLAX_LAYER_NUM);
			
			this.bg_gradient = draw_gradient_layer();			
			addChild(this.bg_gradient);
			
			for (var i:uint = 0; i < this.PARALLAX_LAYER_NUM; i++)
			{								
				this.parallax_layers[i] = draw_star_layer();				
				addChild(this.parallax_layers[i]);
			}	
		}
		
		
		public static function get_instance():StarlingSprite
		{
			return instance;
		}
		
		
		private function draw_gradient_layer():Image
		{
			var s:flash.display.Sprite = new flash.display.Sprite();			
						
			s.graphics.beginFill(0x445577, 1);
			var gradient_matrix:Matrix = new Matrix();
			gradient_matrix.createGradientBox(1100, 1000, 5*Math.PI/4, -250, -250);
			s.graphics.beginGradientFill("radial", [0x334059, 0x161b26], [1, 1], [0, 0xff], gradient_matrix, "pad", "rgb", 0.5);
			s.graphics.drawRect(0, 0, Capabilities.screenResolutionX, Capabilities.screenResolutionY);
			s.graphics.endFill();		
			
			var bmd:BitmapData = new BitmapData(Capabilities.screenResolutionX, Capabilities.screenResolutionY, true, 0);			
			bmd.draw(s);									
			var texture:Texture = Texture.fromBitmapData(bmd);			
			return new Image(texture);			
		}
		
		
		private function draw_star_layer():Image
		{
			var s:flash.display.Sprite = new flash.display.Sprite();	
			
			var x_offset:Number = 100;
			var x_range:Number = Capabilities.screenResolutionX + x_offset*2;
			
			var y_offset:Number = 100;
			var y_range:Number = Capabilities.screenResolutionY + y_offset*2;
			
			var color_variation:uint = 30;
			
			for (var i:uint = 0; i < STAR_NUMBER; i++)
			{
				var x:Number = Math.random() * x_range - x_offset;
				var y:Number = Math.random() * y_range - y_offset;				
				var r:uint = uint(Math.random() * color_variation + (255-color_variation));
				var g:uint = uint(Math.random() * color_variation + (255-color_variation));
				var b:uint = uint(Math.random() * color_variation + (255-color_variation));
				var a:Number = Math.random() * 0.5 + 0.4;
				var size:Number = 0.5 + Math.random() * 1.5;
				
				s.graphics.beginFill((r<<16)+(g<<8)+b, a); 							
				s.graphics.drawCircle(x, y, size);
				s.graphics.endFill();
			}	
			
			var bmd:BitmapData = new BitmapData(Capabilities.screenResolutionX, Capabilities.screenResolutionY, true, 0);			
			bmd.draw(s);									
			var texture:Texture = Texture.fromBitmapData(bmd);			
			return new Image(texture);			
		}
		
		
		public function parallax_move(v:Point, distance:Number):void
		{			
			for (var i:uint = 0; i < this.PARALLAX_LAYER_NUM; i++)
			{								
				var layer:Image = this.parallax_layers[i];
				layer.x -= v.x * distance * i / PARALLAX_LAYER_NUM;
				layer.y -= v.y * distance * i / PARALLAX_LAYER_NUM;
			}	
		}
	}

}